package cn.tedu.tmall.common.web;

import cn.tedu.tmall.common.ServiceCodeEnum;
import lombok.AccessLevel;
import lombok.Data;
import lombok.experimental.FieldDefaults;

import java.io.Serializable;
@Data
@FieldDefaults(level = AccessLevel.PRIVATE)
public class JsonResult<T> implements Serializable {


    Integer code;

    String msg;

    T data;
    public static JsonResult ok(){
        JsonResult jsonResult = new JsonResult();
        JsonResult ok = JsonResult.ok(ServiceCodeEnum.OK);
        return ok;
    }

    public static JsonResult ok(Object list){
        JsonResult jsonResult = new JsonResult();
        JsonResult ok = JsonResult.ok(ServiceCodeEnum.OK);
        ok.setData(list);
        return ok;
    }
    /**
     * 封装
     */
    private static JsonResult ok(ServiceCodeEnum serviceCodeEnum){
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(serviceCodeEnum.getCode());
        jsonResult.setMsg(serviceCodeEnum.getMsg());
        return jsonResult;
    }
    public static  JsonResult fail( String msg) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(ServiceCodeEnum.ERROR.getCode());
        jsonResult.setMsg(msg);
        return jsonResult;
    }

    public static  JsonResult fail(ServiceCodeEnum serviceCodeEnum) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(serviceCodeEnum.getCode());
        jsonResult.setMsg(serviceCodeEnum.getMsg());
        return jsonResult;
    }
    public static  JsonResult fail(Integer code,String msg) {
        JsonResult jsonResult = new JsonResult();
        jsonResult.setCode(code);
        jsonResult.setMsg(msg);
        return jsonResult;
    }
}
